//
// Format.h
//
// $Id: //poco/1.4/Foundation/include/Poco/Format.h#2 $
//
// Library: Foundation
// Package: Core
// Module:  Format
//
// Definition of the format freestanding function.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Foundation_Format_INCLUDED
#define Foundation_Format_INCLUDED

#include <stdio.h>
#include <memory>
#include <cstring>
#include <stdarg.h>
#include "Exception.h"
#include "Poco/Foundation.h"
using namespace std;
namespace Poco {
// 暂时就不用传引用参数了，即不用考虑完美转发了,C_Style,%s needs .c_str()
inline string Foundation_API format(const char *strFmt,...) throw(InvalidArgumentException)
{
	const int smallBuf = 1024;
    char buf[smallBuf]{0};
	va_list start;
	va_start(start,strFmt);
	int ret = vsnprintf(buf,smallBuf,strFmt,start);
	if (ret >= 0 && ret <= smallBuf)
	{
		va_end(start);
		return std::move(string(buf));
	}
	
	const int bigBuf = 1024*32;
	std::unique_ptr<char> pBuf(new char[bigBuf]);
	ret = vsnprintf(pBuf.get(),bigBuf,strFmt,start);
	if (ret >= 0 && ret <= bigBuf)
	{
		va_end(start);
		return std::move(string(pBuf.get()));
	}
	else
	{
		va_end(start);
		throw InvalidArgumentException("unable to format duo to size");
	}
	va_end(start);
	return move(string());
}

} // namespace Poco


#endif // Foundation_Format_INCLUDED
